﻿#region License
// Copyright © 2004 - 2010 All Right Reserved, Derek Goslin
// 
// 1. Definitions
// The terms "reproduce," "reproduction," "derivative works," and "distribution" have the same meaning here as under U.S. copyright law.
// A "contribution" is the original software, or any additions or changes to the software.
// A "contributor" is any person that distributes its contribution under this license.
// "Licensed patents" are a contributor's patent claims that read directly on its contribution.
// 
// 2. Grant of Rights
// (A) Copyright Grant- Subject to the terms of this license, including the license
//     conditions and limitations in section 3, each contributor grants you a non-exclusive,
//     worldwide, royalty-free copyright license to reproduce its contribution, prepare derivative 
//     works of its contribution, and distribute its contribution or any derivative works that you create.
// (B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations
//     in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free license under its
//     licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of
//     its contribution in the software or derivative works of the contribution in the software.
// 
// 3. Conditions and Limitations
// (A) Reciprocal Grants- For any file you distribute that contains code from the software (in source code or binary format), 
//     you must provide recipients the source code to that file along with a copy of this license, which license will govern that file. 
//     You may license other files that are entirely your own work and do not contain code from the software under any terms you choose.
// (B) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks.
// (C) If you bring a patent claim against any contributor over patents that you claim are infringed by the software,
//     your patent license from such contributor to the software ends automatically.
// (D) If you distribute any portion of the software, you must retain all copyright, patent, trademark,
//     and attribution notices that are present in the software.
// (E) If you distribute any portion of the software in source code form, you may do so only under this license by including a complete copy 
//     of this license with your distribution. If you distribute any portion of the software in compiled or object code form, you may only do 
//     so under a license that complies with this license.
// (F) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees or conditions.
//    You may have additional consumer rights under your local laws which this license cannot change. To the extent permitted under your
//    local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular purpose and non-infringement.
#endregion

using System;

namespace DnEcuDiag.Conversion
{
    public class ME31EODBConversion : Convertor, IConvertor
    {
        private readonly float[] _me31EodbAirTemp;
        private readonly float[] _me31EodbEngineTemp;

        #region IConvertor Members

        public ME31EODBConversion()
        {
            #region ME3.1EODB Temp

            _me31EodbEngineTemp = new[]
                                      {
                                          -41f, -40.8f, -40.6f, -40.4f, -40.2f, -40f, -37.5f, -35f, -32.5f, -30f,
                                          -28.36f, -26.72f, -25.08f,
                                          -23.43f, -21.79f, -20.15f,
                                          -19.04f, -17.92f, -16.81f, -15.69f, -14.58f, -13.46f, -12.35f, -11.23f,
                                          -10.12f, -9.29f, -8.46f,
                                          -7.63f, -6.79f, -5.96f, -5.13f, -4.3f,
                                          -3.47f, -2.64f, -1.8f, -0.97f, -0.14f, 0.45f, 1.05f, 1.64f, 2.23f, 2.82f,
                                          3.42f, 4.01f, 4.6f, 5.2f,
                                          5.79f, 6.38f,
                                          6.98f, 7.57f, 8.16f, 8.75f, 9.35f, 9.94f, 10.44f, 10.94f, 11.45f, 11.95f,
                                          12.45f, 12.95f, 13.45f,
                                          13.96f, 14.46f, 14.96f,
                                          15.46f, 15.96f, 16.47f, 16.97f, 17.47f, 17.97f, 18.47f, 18.98f, 19.48f, 19.98f
                                          , 20.43f, 20.89f,
                                          21.34f, 21.8f, 22.25f, 22.71f,
                                          23.16f, 23.62f, 24.07f, 24.53f, 24.98f, 25.44f, 25.9f, 26.35f, 26.81f, 27.27f,
                                          27.73f, 28.19f,
                                          28.65f, 29.1f, 29.56f, 30.02f,
                                          30.45f, 30.89f, 31.32f, 31.75f, 32.18f, 32.62f, 33.05f, 33.48f, 33.91f, 34.35f
                                          , 34.78f, 35.21f,
                                          35.64f, 36.08f, 36.51f, 36.94f,
                                          37.37f, 37.81f, 38.24f, 38.67f, 39.1f, 39.54f, 39.97f, 40.41f, 40.84f, 41.28f,
                                          41.71f, 42.15f,
                                          42.58f, 43.02f, 43.45f, 43.89f,
                                          44.32f, 44.76f, 45.19f, 45.63f, 46.06f, 46.5f, 46.93f, 47.37f, 47.8f, 48.24f,
                                          48.67f, 49.11f, 49.54f
                                          , 49.98f, 50.46f, 50.93f,
                                          51.41f, 51.89f, 52.37f, 52.84f, 53.32f, 53.8f, 54.27f, 54.75f, 55.23f, 55.71f,
                                          56.18f, 56.66f,
                                          57.14f, 57.61f, 58.09f, 58.57f,
                                          59.05f, 59.52f, 60f, 60.56f, 61.12f, 61.68f, 62.24f, 62.8f, 63.36f, 63.92f,
                                          64.48f, 65.04f, 65.6f,
                                          66.16f, 66.72f, 67.28f,
                                          67.84f, 68.4f, 68.96f, 69.52f, 70.08f, 70.74f, 71.41f, 72.07f, 72.73f, 73.4f,
                                          74.06f, 74.72f, 75.39f
                                          , 76.05f, 76.71f, 77.38f,
                                          78.04f, 78.7f, 79.37f, 80.03f, 80.86f, 81.69f, 82.52f, 83.35f, 84.18f, 85.02f,
                                          85.85f, 86.68f,
                                          87.51f, 88.34f, 89.17f, 90f,
                                          91f, 91.99f, 92.99f, 93.98f, 94.98f, 95.98f, 96.97f, 97.97f, 98.96f, 99.96f,
                                          101.2f, 102.45f,
                                          103.69f, 104.94f, 106.18f, 107.42f,
                                          108.67f, 109.91f, 111.63f, 113.36f, 115.08f, 116.8f, 118.53f, 120.25f, 122.13f
                                          , 124.01f, 125.88f,
                                          127.76f, 129.64f, 130.19f, 130.73f, 131.28f,
                                          131.82f, 132.37f, 132.91f, 133.46f, 134f, 134.55f, 135.09f, 135.64f, 136.18f,
                                          136.73f, 137.27f,
                                          137.82f, 138.36f, 138.91f, 139.45f, 140
                                      };

            _me31EodbAirTemp = new[]
                                   {
                                       -50.1f, -50.09f, -50.07f, -50.06f, -50.04f, -50.03f, -50.01f, -50f, -48f, -47f,
                                       -45f, -43f, -41f,
                                       -40f, -39f, -37f,
                                       -36f, -35f, -34f, -32f, -31f, -30f, -29f, -28.5f, -28f, -27f, -26f, -25f, -24f,
                                       -23.5f, -23f, -22f,
                                       -21f, -20.5f, -20f, -19f, -18.5f, -18f, -17f, -16.5f, -16f, -15.5f, -15f, -14f,
                                       -13.5f, -13f, -12.5f
                                       , -12f,
                                       -11.5f, -11f, -10.5f, -10f, -9.5f, -9f, -8.5f, -8f, -7.5f, -7f, -6.5f, -6f, -5.5f
                                       , -5f, -4.5f, -4f,
                                       -3.5f, -3f, -2.5f, -2f, -1.5f, -1f, -0.5f, 0f, 0.33f, 0.67f, 1f, 1.5f, 2f, 2.5f,
                                       3f, 3.33f,
                                       3.67f, 4f, 4.5f, 5f, 5.5f, 6f, 6.33f, 6.67f, 7f, 7.5f, 8f, 8.33f, 8.67f, 9f, 9.5f
                                       , 10f,
                                       10.33f, 10.67f, 11f, 11.5f, 12f, 12.33f, 12.67f, 13f, 13.5f, 14f, 14.33f, 14.67f,
                                       15f, 15.5f, 16f,
                                       16.33f,
                                       16.67f, 17f, 17.5f, 18f, 18.5f, 19f, 19.33f, 19.67f, 20f, 20.5f, 21f, 21.33f,
                                       21.67f, 22f, 22.5f,
                                       23f,
                                       23.33f, 23.67f, 24f, 24.5f, 25f, 25.33f, 25.67f, 26f, 26.5f, 27f, 27.5f, 28f,
                                       28.33f, 28.67f, 29f,
                                       29.5f,
                                       30f, 30.5f, 31f, 31.5f, 32f, 32.33f, 32.67f, 33f, 33.5f, 34f, 34.5f, 35f, 35.5f,
                                       36f, 36.5f, 37f,
                                       37.5f, 38f, 38.5f, 39f, 39.5f, 40f, 40.5f, 41f, 41.5f, 42f, 42.5f, 43f, 43.5f,
                                       44f, 44.5f, 45f,
                                       45.5f, 46f, 46.5f, 47f, 48f, 48.5f, 49f, 49.5f, 50f, 50.5f, 51f, 52f, 52.5f, 53f,
                                       54f, 54.5f,
                                       55f, 56f, 56.5f, 57f, 58f, 58.5f, 59f, 60f, 61f, 61.5f, 62f, 63f, 64f, 65f, 65.5f
                                       , 66f,
                                       67f, 68f, 69f, 70f, 71f, 72f, 73f, 74f, 75f, 76f, 77f, 78f, 79f, 80f, 82f, 83f,
                                       84f, 86f, 87f, 89f, 90f, 92f, 93f, 95f, 96.67f, 98.33f, 100f, 102.5f, 105f,
                                       107.5f, 110f, 112.5f,
                                       115f, 120f, 122.5f, 125f, 130f, 135f, 140f, 145f, 150f, 150.01f, 150.03f, 150.04f
                                       , 150.05f, 150.06f,
                                       150.08f, 150.09f
                                   };

            #endregion
        }

        public float Convert(int conversionId, byte[] rawData)
        {
            float result = 0;
            UInt32 rawValue = ConvertByteArray(rawData);

            switch (conversionId)
            {
                case 0:
                    result = rawValue;
                    break;
                case 600:
                    result = rawValue * .1f;
                    break;
                case 601:
                    result = rawValue;
                    break;
                case 602:
                    result = rawValue;
                    break;
                case 603:
                    result = rawValue * 100;
                    break;
                case 604:
                    result = rawValue * 40;
                    break;
                case 605:
                    result = rawValue;
                    break;
                case 606:
                    result = rawValue * .1f;
                    break;
                case 607:
                    result = rawValue;
                    break;
                case 608:
                    result = _me31EodbEngineTemp[rawValue];
                    break;
                case 609:
                    result = _me31EodbAirTemp[rawValue];
                    break;
                case 610:
                    result = rawValue * .1f;
                    break;
                case 611:
                    result = (int)(rawValue - 104) * 0.75f;
                    break;
                case 612:
                    result = rawValue * 2.66f;
                    break;
                case 613:
                    result = rawValue * 21.33f;
                    break;
                case 614:
                    result = (rawValue * 80.916f) / 256;
                    break;
                case 615:
                    result = rawValue * 0.75f;
                    break;
                case 616:
                    result = rawValue * 2.66f;
                    break;
                case 617:
                    result = rawValue * 85.33f;
                    break;
                case 618:
                    result = (int)(rawValue - 2) / 7.7f;
                    break;
                case 619:
                    result = rawValue * 4.88f;
                    break;
                case 620:
                    result = rawValue * 0.0942f;
                    break;
                case 621:
                    result = rawValue / 16f;
                    break;
                case 622:
                    result = rawValue / 16f;
                    break;
                case 623:
                    result = rawValue * 0.0942f;
                    break;
                case 624:
                    result = rawValue * 0.1f;
                    break;
                case 625:
                    result = rawValue;
                    break;
                case 626:
                    result = rawValue * 80.916f / 32767;
                    break;
                case 627:
                    result = (int)(rawValue * 4) - 48;
                    break;
                case 628:
                    result = (rawValue * 100f) / 255;
                    break;
                case 629:
                    result = rawValue * 2.66f;
                    break;
                case 630:
                    result = _me31EodbEngineTemp[rawValue];
                    break;
                case 631:
                    result = (int)rawValue - 40;
                    break;
                case 632:
                    result = (int)(rawValue * 4) - 48;
                    break;
                case 633:
                    result = ((int)rawValue - 32767) / 5242f;
                    break;
                case 634:
                    result = rawValue * 1f / 2621;
                    break;
                case 635:
                    result = rawValue * 100f / 215;
                    break;
                case 636:
                    result = rawValue;
                    break;
                case 637:
                    result = ((float)rawValue / 65536) + 0.50f;
                    break;
                case 638:
                    result = ((float)rawValue / 65536) + 0.50f;
                    break;
                case 639:
                    result = (rawValue * 80.916f) / 32767;
                    break;
                case 640:
                    result = (rawValue * 80.916f) / 32767;
                    break;
                case 641:
                    result = rawValue / 128f;
                    break;
                case 642:
                    result = rawValue * 2.66f;
                    break;
                case 643:
                    result = rawValue - 40;
                    break;
                case 644:
                    result = (rawValue * 4) - 48;
                    break;
                case 645:
                    result = (rawValue * 4) - 48;
                    break;
                case 646:
                    result = rawValue * 1.25f / 256;
                    break;
                case 647:
                    result = rawValue / (65536 + 0.5f);
                    break;
                case 648:
                    if ((rawValue & 0x8000) == 0)
                    {
                        result = rawValue * 2.66f;
                    }
                    else
                    {
                        rawValue = (~rawValue) & 0xFFFF; // same as 16bit int invert.  
                        result = (rawValue + 1) * -2.66f;
                    }
                    break;
                case 649:
                    result = rawValue * 80.916f / 256;
                    break;
                case 650:
                    result = rawValue / 327.67f;
                    break;
                case 651:
                    result = (rawValue * 100) / 1023f;
                    break;
                case 652:
                    result = rawValue * 80.916f / 32767;
                    break;
                case 653:
                    result = rawValue * 19f;
                    break;
                case 654:
                    result = rawValue * 5 / 256f;
                    break;
                case 655:
                    result = rawValue;
                    break;
                case 656:
                    result = rawValue * 2f;
                    break;
                case 657:
                    result = rawValue * 10f;
                    break;
            }

            return result;
        }

        #endregion
    }
}
